﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Threading;
using MbWsClient.ServiceClasses;
using MbWsClient.Service.Enums;

namespace MbWsClient.Classes
{
    public class CollectionItem : Item
    {
        public CollectionItem(BasicCollectionItem item)
            : base(item)
        {
            Children = item.Children;
        }

        public IEnumerable<BasicItem> Children;

        private int _lastSelectedIndex;
        public int LastSelectedIndex
        {
            get { return _lastSelectedIndex; }
            set 
            { 
                _lastSelectedIndex = value;
                lock (this)
                {
                    ThreadPool.QueueUserWorkItem(o => { Save(); });
                }
            }
        }

        public void Save()
        {
            Save(new FileInfo(Path.Combine(AppSettings.ItemCacheDir.FullName, Id.ToString() + ".xml")));
        }

        public void Save(FileInfo fileLocation)
        {
            XDocument doc = new XDocument();
            XElement element = new XElement("Data",
                                        GetElement(this),
                                        new XElement("LastSelectIndex", LastSelectedIndex),
                                        new XElement("Children",
                                            from bob in Children
                                            select GetElement(bob)
                                            )
                                            );
            doc.Add(element);
            try
            {
                doc.Save(fileLocation.FullName);
            }
            catch (Exception) { }
        }

        public static CollectionItem Parse(FileInfo file)
        {
            XDocument doc = XDocument.Load(file.FullName);
            var data = doc.Element("Data");
            var mainElement = data.Element("BasicItem");
            BasicItem item = GetItem(mainElement);
            var children = from bob in data.Element("Children").Descendants("BasicItem")
                           select GetItem(bob);
            BasicCollectionItem collItem = new BasicCollectionItem();
            collItem.Id = item.Id;
            collItem.Name = item.Name;
            collItem.Type = item.Type;
            collItem.Children = children.ToArray();
            CollectionItem collection = new CollectionItem(collItem);
            XElement lastSelectedNode = data.Element("LastSelectIndex");
            if (lastSelectedNode != null)
            {
                int lastSelectedIndex;
                if (int.TryParse(lastSelectedNode.Value, out lastSelectedIndex))
                    collection.LastSelectedIndex = lastSelectedIndex;
            }
            return collection;
        }

        private static BasicItem GetItem(XElement element)
        {
            BasicItem item = new BasicItem();
            item.Id = new Guid(element.Element("Id").Value);
            item.Name = element.Element("Name").Value;
            item.Type = element.Element("Type").Value.ToEnum<MediaTypes>();
            return item;
        }

        private static XElement GetElement(BasicItem item)
        {
            XElement element = new XElement("BasicItem",
                                    new XElement("Id", item.Id),
                                    new XElement("Type", item.Type),
                                    new XElement("Name", item.Name));
            return element;
        }        
    }
}
